package com.ruoyi.system.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.TGoodsComments;
import com.ruoyi.system.service.ITGoodsCommentsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商品评论信息Controller
 * 
 * @author ruoyi
 * @date 2024-05-29
 */
@RestController
@RequestMapping("/buyers/comments")
public class TGoodsCommentsController extends BaseController
{
    @Autowired
    private ITGoodsCommentsService tGoodsCommentsService;

    /**
     * 查询商品评论信息列表
     */
    @GetMapping("/list")
    public TableDataInfo list(TGoodsComments tGoodsComments)
    {
        startPage();
        List<TGoodsComments> list = tGoodsCommentsService.selectTGoodsCommentsList(tGoodsComments);
        return getDataTable(list);
    }
    /**
     * 根据id获取商品评论信息列表
     */
    @GetMapping("/selectByGoodAndUser")
    public AjaxResult getByGoodandUser(TGoodsComments tGoodsComments)
    {
        startPage();
        tGoodsComments.setUserId(SecurityUtils.getUserId());
        System.out.println(tGoodsComments.toString());
        List<TGoodsComments> list = tGoodsCommentsService.selectTGoodsCommentsList(tGoodsComments);
        if(list.size()==0)
            list.add(tGoodsComments);
        return success(list.get(0));
    }

    @GetMapping("/selectByGoodId")
    public TableDataInfo getByGoodId(TGoodsComments tGoodsComments)
    {
        startPage();
        List<TGoodsComments> list = tGoodsCommentsService.selectTGoodsCommentsList(tGoodsComments);
        return getDataTable(list);
    }
    /**
     * 导出商品评论信息列表
     */
    @Log(title = "商品评论信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TGoodsComments tGoodsComments)
    {
        List<TGoodsComments> list = tGoodsCommentsService.selectTGoodsCommentsList(tGoodsComments);
        ExcelUtil<TGoodsComments> util = new ExcelUtil<TGoodsComments>(TGoodsComments.class);
        util.exportExcel(response, list, "商品评论信息数据");
    }

    /**
     * 获取商品评论信息详细信息
     */
    @GetMapping(value = "/{commentId}")
    public AjaxResult getInfo(@PathVariable("commentId") String commentId)
    {
        return success(tGoodsCommentsService.selectTGoodsCommentsByCommentId(commentId));
    }

    /**
     * 新增商品评论信息
     */
    @Log(title = "商品评论信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TGoodsComments tGoodsComments)
    {
        tGoodsComments.setUserId(SecurityUtils.getUserId());
        tGoodsComments.setCommentId(SecurityUtils.getUserId()+"_"+System.currentTimeMillis());
        return toAjax(tGoodsCommentsService.insertTGoodsComments(tGoodsComments));
    }

    /**
     * 修改商品评论信息
     */
    @Log(title = "商品评论信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TGoodsComments tGoodsComments)
    {
        return toAjax(tGoodsCommentsService.updateTGoodsComments(tGoodsComments));
    }

    /**
     * 删除商品评论信息
     */
    @Log(title = "商品评论信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{commentIds}")
    public AjaxResult remove(@PathVariable String[] commentIds)
    {
        return toAjax(tGoodsCommentsService.deleteTGoodsCommentsByCommentIds(commentIds));
    }
}
